// ========================================================
// UMmu and UCGO funcs for Shuttle PB Mk2
// Made by SolarLiner
// ========================================================
#include "ShuttlePBMk2.h"

void ShuttlePBMk2::InitUMmu()
{
	oapiWriteLog("PB2/Sys>Query.Init.UMmu");
	
	Crew.InitUmmu(GetHandle());// Must always be the first one
	Crew.DefineAirLockShape(TRUE, -2.55,-1.46, -1.82,2.21, -4.12,-2.46);
	Crew.SetMembersPosRotOnEVA(_V(-2.0, 0.18, -3.32), _V(0, 0, 0));
	Crew.SetMaxSeatAvailableInShip(4);// Max Numbers of UMMU for this ship
	// Add four default members for when the ship is spawned by scenario editor
	Crew.AddCrewMember("Peter Falcon",41,65,74,"Capt");
	Crew.AddCrewMember("Fanny Gorgeous",27,67,55,"Eng");
	Crew.AddCrewMember("George HealGood",15,70,45,"Doc");
	// Define some other Crew variables
	Crew.SetEjectPosRotRelSpeed(_V(3.32, 1.77, 3.29), _V(0, 0, 0), _V(0, 50, -10));
	Crew.SetO2ReserveWhenEjecting(10*60);
	Crew.SetO2ReserveWhenEvaing(2*60);
	iSelectedUmmuMember=0;		// Initialize of select member variable
	// The HUD display method variables
	cUmmuHudDisplay[0] =0;		// Initialisation of UMmu hud char variable
	dHudMessageDelay =0;		// Initialisation of UMmu delay variable
	cAddUMmuToVessel[0]=0;		// UMmu Add vessel without scenery editor variable
	dOldVertSpeed = 0;			// UMmu vertical speed to kill crew
	iAliveUMMU = 0;				// UMmu number of crew with pulse > 0
}

void ShuttlePBMk2::InitUCGO()
{
	oapiWriteLog("PB2/Sys>Query.Init.UCGO");
	// Initialisation, cargo slot pos n rot
	hUcgo.Init(GetHandle());
	hUcgo.DeclareCargoSlot(0, _V(-0.94, 1.73, -3.26), _V(0, 0,-11.198*RAD));
	hUcgo.DeclareCargoSlot(1, _V (0.94, 1.73, -3.26), _V(0, 0, 11.198*RAD));
	hUcgo.DeclareCargoSlot(2, _V(-1.67, 0.37, -1.83), _V(0, 0, 1));
	hUcgo.DeclareCargoSlot(3, _V (1.67, 0.37, -1.83), _V(0, 0,-1));

	// Parameters Settings
	hUcgo.SetReleaseSpeedInSpace(0.10f);
	hUcgo.SetMaxCargoMassAcceptable(1750);
	hUcgo.SetGrappleDistance(20);
	hUcgo.SetGlobalGroundReleasePos(_V(4, -2, -4));

	// Vars init
	iSelectedCargo = -1;

}

int ShuttlePBMk2::KillUMmu()
{
	int C=0;
	int nb=0;
	// we touched ground with more than -20 m/s, sorry dude, time to kill you all :(
	for(C=0;C<Crew.GetCrewTotalNumber();C++)
	{
		Crew.SetCrewMemberPulseBySlotNumber(C,0);// set cardiac pulse to zero
		nb++;
	}
	return nb;
}

bool UMmuCrewAddCallback(void *id, char *str, void *data)
{
	if(strlen(str)<2||strlen(str)>38)
		return false;
	char *cPtr=(char*)data;if(*cPtr==2){*cPtr=3;strcpy(cPtr+2,str);}
	else if(*cPtr==4){*cPtr=5;strcpy(cPtr+42,str);}
	else if(*cPtr==6){*cPtr=7;strcpy(cPtr+82,str);}return true;
}

void ShuttlePBMk2::AddUMmuToVessel(BOOL bStartAdding)
{
	if(bStartAdding==FALSE&&cAddUMmuToVessel[0]==0)
		return;
	if(bStartAdding==TRUE){
		int salut=sizeof(cAddUMmuToVessel);
		memset(cAddUMmuToVessel,0,sizeof(cAddUMmuToVessel));
		cAddUMmuToVessel[0]=1;
	}
	else if(cAddUMmuToVessel[0]==1){
		cAddUMmuToVessel[0]=2;
		oapiOpenInputBox ("Enter new crew's name (or escape)",UMmuCrewAddCallback,0,30,(void*)cAddUMmuToVessel);
	}
	else if(cAddUMmuToVessel[0]==3){
		cAddUMmuToVessel[0]=4;
		oapiOpenInputBox ("Enter crew's age",UMmuCrewAddCallback,0,30,(void*)cAddUMmuToVessel);
	}
	else if(cAddUMmuToVessel[0]==5){
		cAddUMmuToVessel[0]=6;
		oapiOpenInputBox ("Enter function (Capt,Sec,Vip,Sci,Doc,Tech,Crew,Pax,...)",UMmuCrewAddCallback,0,30,(void*)cAddUMmuToVessel);
	}
	else if(cAddUMmuToVessel[0]==7){
		cAddUMmuToVessel[0]=0;
		int Age=max(5,min(100,atoi(&cAddUMmuToVessel[42])));
		if(Crew.AddCrewMember(&cAddUMmuToVessel[2],Age,70,70,&cAddUMmuToVessel[82])==TRUE){
			sprintf(SendHudMessage(),"Crew \"%s\" aged %i added to vessel",&cAddUMmuToVessel[2],Age);
			DefineThrusters();
		}
		else{
			strcpy(SendHudMessage(),"ERROR: Crew not added (vessel full?)");
		}
	}
}